package com.example.demo;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;

@Configuration
public class Config {

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {

        //转发到http://localhost:11308/
        return builder.routes()
                //断言路径以ichiva开始
            .route("path",r->r.path("/ichiva/**")
                    //去掉1个路径前缀
                    .filters(f -> f.stripPrefix(1)
                            //配置局部过滤器
                            .filters(new SimpleGatewayFilter()))
                .uri("http://www.ichiva.cn/")).build(); //转发目标服务器

//        //断言头部信息有server并且匹配ichiva
//        //转发到http://localhost:11308/
//        locator = builder.routes()
//                .route("header",r->r.header("server","ichiva")
//                        .uri("http://localhost:11308/")).build();
//
//        //断言时间,120后
//        //转发到http://localhost:11308/
//        locator = builder.routes()
//                .route("header",r->r.after(ZonedDateTime.now().plusSeconds(120))
//                        .uri("http://localhost:11308/")).build();
//
//        //断言时间,120以前
//        //转发到http://localhost:11308/
//        locator = builder.routes()
//                .route("header",r->r.before(ZonedDateTime.now().plusSeconds(120))
//                        .uri("http://ichiva.cn/")).build();

        //域名略
        //请求方法略

//        return locator;
    }
}
